﻿namespace ChinaUnicom.ViewModels
{
    using Avatar;
    using Avatar.Schedule;
    using ChinaUnicom.Controls;
    using ChinaUnicom.Models;
    using ChinaUnicom.Net;
    using ChinaUnicom.Tasks;
    using System;
    using System.ComponentModel;
    using System.Windows;

    public class SearchContext : PageContext, INotifyPropertyChanged
    {
        ITaskScheduler scheduler;

        public SearchContext()
        {
            scheduler = SchedulerFactory.Create(SchedulerType.Serial);
            scheduler.Error += OnSchedulerError;
            scheduler.StateChanged += OnSchedulerStateChanged;

            PointItems = Context.Current.Point.Items;

            PointPullrefresh = "下拉可以刷新";

            PointProgressEnabled = false;

            if (NetHelper.Network.IsNetworkAvailable)
            {
                if (Context.Current.Point.Occurred.IsOutdated(24 * 60))
                {
                    scheduler.Schedule(GeneratePointTask());
                }
            }
        }
        
        public void ForceUpdatePoint()
        {
            if (NetHelper.Network.IsNetworkAvailable)
            {
                scheduler.Schedule(GeneratePointTask());
            }
        }

        #region Tasks

        void OnSchedulerError(object sender, FailedEventArgs e)
        {
            Action<FailedEventArgs> action = args =>
            {
                Message.Tips(args.Exception.Message, "温馨提示");
            };

            action.UIThreadInvoke(e);
        }

        void OnSchedulerStateChanged(object sender, StateChangedArgs e)
        {
            if (e.State == ScheduleState.Done)
            {
                Action action = () =>
                {
                };

                action.UIThreadInvoke();
            }
        }

        ITask GenerateAutologinTask()
        {
            ITask task = new AutologinTask();
            task.Failed += OnFailed;

            return task;
        }

        ITask GeneratePointTask()
        {
            if (!Authentication.Current.Signed)
            {
                scheduler.Schedule(GenerateAutologinTask());
            }

            ITask task = new PointTask();
            task.Started += OnTaskStated;
            task.Failed += OnFailed;
            task.Completed += OnTaskCompleted;

            return task;
        }

        void OnTaskStated(object sender, EventArgs e)
        {
            Action<ITask> action = task =>
            {
                switch (task.GetType().Name.ToLower())
                {
                    case "pointtask":
                        PointProgressEnabled = true;
                        break;

                }
            };

            action.UIThreadInvoke(sender as ITask);
        }

        void OnTaskCompleted(object sender, EventArgs e)
        {
            Action<ITask> action = task =>
            {
                switch (task.GetType().Name.ToLower())
                {
                    case "pointtask":
                        var p = task as PointTask;
                        Context.Current.Point = p.Point;
                        PointItems.Clear();
                        PointItems.Add(Context.Current.Point.Items);
                        PointProgressEnabled = false;
                        break;
                }
            };

            action.UIThreadInvoke(sender as ITask);
        }

        void OnFailed(object sender, FailedEventArgs e)
        {
            Action<FailedEventArgs> action = args =>
            {
                Message.Tips(args.Exception.Message, "温馨提示");
            };

            action.UIThreadInvoke(e);
        }

        #endregion
        
        ItemCollection pointItems;
        public ItemCollection PointItems
        {
            get { return pointItems; }
            set
            {
                if (value != this.pointItems)
                {
                    pointItems = value;
                    NotifyPropertyChanged("PointItems");
                }
            }
        }
        
        bool pointProgressEnabled;
        public bool PointProgressEnabled
        {
            get { return pointProgressEnabled; }
            set
            {
                if (value != pointProgressEnabled)
                {
                    pointProgressEnabled = value;
                    NotifyPropertyChanged("PointProgressEnabled");
                }
            }
        }


        string pointPullrefresh;
        public string PointPullrefresh
        {
            get { return pointPullrefresh; }
            set
            {
                if (value != pointPullrefresh)
                {
                    pointPullrefresh = value;
                    NotifyPropertyChanged("PointPullrefresh");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
